import { Injectable } from '@angular/core';
import { Profile } from '../../framework/login/models/profile.model';
import { Claims } from '../models/claims.model';

@Injectable()
export class StorageService {

    //this storage element is overwritten on token expiration. Must be directly read from localStorage because we need persistance
    get authData(): any {
        return JSON.parse(localStorage.getItem('sust:a'));
    }

    public setProfile(data: Profile) {
        localStorage.setItem('sust:a:user', JSON.stringify(data));
    }

    public resourcePermissions: Claims[];

    public userInfo: Profile;

    constructor() {
        this.resourcePermissions = JSON.parse(localStorage.getItem('sust:a:claims'));
        this.userInfo = JSON.parse(localStorage.getItem('sust:a:user'));
    }

    private static _instance: StorageService = null;

    public static get(): StorageService {
        if (StorageService._instance === null)
            StorageService._instance = new StorageService();
        return StorageService._instance;
    }

    public static markExpired(): void {
        let auth = StorageService._instance.authData;
        auth.loginTime = new Date(2000, 1, 1);
        localStorage.setItem('sust:a', JSON.stringify(auth));
    }
}